package www.shuang.common.controller;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;

import www.shuang.common.util.DateFormatUtil;
import www.shuang.common.util.EncryptUtil;
import www.shuang.common.util.PageInfoUtil;
import www.shuang.common.util.RegexUtil;
import www.shuang.common.util.VerifyCodeUtils;


public class BaseController
{		@Resource
		protected EncryptUtil encryptUtil;
		@Resource
        protected RegexUtil regexUtil;
		@Resource
		protected DateFormatUtil dateFormatUtil;
		
		protected String info;
		//生成验证码
		@RequestMapping("/code.htm")
		public String code(HttpServletRequest request,HttpServletResponse response){
			
			HttpSession session=request.getSession();
			String code=this.regexUtil.andString(4);
			try
			{
				VerifyCodeUtils.outputImage(100, 50, response.getOutputStream(), code);
			} catch (IOException e)
			{
				e.printStackTrace();
			}
			
			/* 将图片中的验证放到session中,用来比较 */
			session.setAttribute("code", code);
			return null ;
		}
		
		//生成分页对象
		protected PageInfoUtil proccedPage(HttpServletRequest request)
		{
			PageInfoUtil pageInfoUtil =new PageInfoUtil();
			String currentPage=request.getParameter("currentPage");
			String pageSize=request.getParameter("pageSize");
			try
			{
				pageInfoUtil.setCurrentPage(Integer.valueOf(currentPage));
				pageInfoUtil.setPageSize(Integer.valueOf(pageSize));
			} catch (NumberFormatException e)
			{
			}
			return pageInfoUtil;
		}
		
		/**
		 * 拼装JSON
		 * JUI框架需要的JSON
		 * 
		 * String res = "{\"statusCode\":\"200\", \"message\":\""+ info +"\", \"navTabId\":\"\", 
		 * \"rel\":\"\", \"callbackType\":\"\", \"forwardUrl\":\"\", \"confirmMsg\":\"\"}" ;
		 * 
		 * @param request
		 * @param info
		 * @return
		 */
		protected JSONObject returnJuiJSON(HttpServletRequest request,  String info)
		{
			/* JSON中的其它信息从request中获取,(JSP页面上) */
			String navTabId = request.getParameter("navTabId");
			String rel = request.getParameter("rel");
			String callbackType = request.getParameter("callbackType");
			String forwardUrl = request.getParameter("forwardUrl");
			String confirmMsg = request.getParameter("confirmMsg");
			
			if(navTabId == null)
			{
				navTabId = "" ; 
			}
			if(rel == null)
			{
				rel = "" ; 
			}
			if(callbackType == null)
			{
				callbackType = "" ; 
			}
			if(forwardUrl == null)
			{
				forwardUrl = "" ; 
			}
			if(confirmMsg == null)
			{
				confirmMsg = "" ; 
			}
			JSONObject resultJSON = new JSONObject();
			resultJSON.put("statusCode", "200");
			resultJSON.put("message", info);
			resultJSON.put("navTabId", navTabId);
			resultJSON.put("rel", rel);
			resultJSON.put("callbackType", callbackType);
			resultJSON.put("forwardUrl", forwardUrl);
			resultJSON.put("confirmMsg", confirmMsg);
			return resultJSON;
		}
}
